package chuishop.yiqimaicai.controller;

import chuishop.yiqimaicai.pojo.Shopcar;
import chuishop.yiqimaicai.service.ShopcarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ShopcarController {

    @Autowired
    private ShopcarService shopcarService;

    @RequestMapping("selectShopcarByUid")
    @ResponseBody
    public List<Shopcar> selectShopcarByUid(Integer uid){
        List<Shopcar> list = shopcarService.selectShopcarByUid(uid);
        return list;
    }

    //添加购物车
    @RequestMapping("addShopcar")
    @ResponseBody
    public Map<Object, Object> addShopcar(@RequestBody Shopcar shopcar){

        Map<Object, Object> result = new HashMap<>();

        boolean flag = shopcarService.addShopcar(shopcar);
        if(flag){
            result.put("status", "200");
            result.put("msg","添加成功");
        }else {

            result.put("status", "500");
            result.put("msg", "添加失败");
        }
        return result;
    }

    //更新购物车
    @RequestMapping("updateShopcar")
    @ResponseBody
    public Map<Object, Object> updateShopcar(@RequestBody Shopcar shopcar){

        Map<Object, Object> result = new HashMap<>();

        boolean flag = shopcarService.updateShopcar(shopcar);
        if(flag){
            result.put("status", "200");
            result.put("msg","添加成功");
        }else {

            result.put("status", "500");
            result.put("msg", "添加失败");
        }
        return result;
    }

    //删除购物车
    @RequestMapping("delShopcar")
    @ResponseBody
    public Map<Object, Object> delShopcar( String ids){

        String idstr[] = ids.split(",");

        Map<Object, Object> result = new HashMap<>();

        boolean flag = shopcarService.delShopcar(idstr);
        if(flag){
            result.put("status", "200");
            result.put("msg","添加成功");
        }else {

            result.put("status", "500");
            result.put("msg", "添加失败");
        }
        return result;
    }

}
